﻿app.factory("GameList", function () {
    return [];
})

app.controller("ngGameIndexController", function ($scope, $location, $http, GameList) {
    $scope.games = GameList;
    //toggle
    $scope.collapseGame = function (game) {
        for (var i in $scope.game) {
            delete $scope.game[i].collapseGame;
        }
        if (game.collapseGame) {
            delete game.collapseGame;
        } else {
            game.collapseGame = true;
        };
    };

    $scope.getGameInfo = function (game) {
        //console.log("inside get game info");
        //if (game.UserList) {
        $http.get("/api/game/available/" + game.Id).success(function (data) {
            console.log(data);
            game.UserList = data;
        }).error(function (data) {
            console.log(data)
        });
        //}
    };

    $scope.rentGame = function (game) {
        if ($scope.username) {
            $http.post("/api/rental/rent/" + game.Id).success(function (data) {
                $location.path('/Account');
            });
        } else {
            $("#loginModal").modal("show");
        }
    };
    $scope.buyGame = function (game) {
        if ($scope.username) {
            $http.post("/api/rental/buy/" + game.Id).success(function (data) {
                $location.path('/Account');
            });
        } else {
            $("#loginModal").modal("show");
        }
    };
    $scope.tradeGame = function (game) {
        if ($scope.username) {
            alert("Trade email request pending...")
        } else {
            $("#loginModal").modal("show");
        }
    };


    $http.get("/api/game").success(function (data) {
        //console.log(data);
        $scope.games = data;
    }).error(function (data) {
        console.log(JSON.stringify(data))
    });

    $scope.selectgenre = function (i) {
        $scope.genre = "";
        if (i == 1) {
            $scope.genre = "Sports";
        } else if (i == 2) {
            $scope.genre = "Shooter";
        } else if (i == 3) {
            $scope.genre = "Action";
        } else if (i == 4) {
            $scope.genre = "Adventure";
        } else if (i == 5) {
            $scope.genre = "Puzzle";
        } else if (i == 6) {
            $scope.genre = "Role-Playing";
        }
    }

});